//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.game.click.MarkedCanvas;
import tv.dyndns.kishibe.client.geom.Point;
import tv.dyndns.kishibe.client.packet.PacketProblemData;
import tv.dyndns.kishibe.client.util.SerialTimer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class WidgetInputClick extends WidgetInputBase implements MouseDownHandler, ClickHandler {
	private static WidgetInputClick INSTANCE = null;
	private boolean throughClick = false;
	private final MarkedCanvas canvas = new MarkedCanvas();
	private final Button buttonOk = new Button("OK", this);
	private int mouseX = Point.INVALID;
	private int mouseY = Point.INVALID;
	private int circleMarkId = MarkedCanvas.REGISTER;
	private final Image image;
	private final Timer timerUpdate = new SerialTimer() {
		public void run() {
			canvas.update();
		}
	};
	private static final int UPDATE_DURATION = 1000;

	public static WidgetInputClick getInstance() {
		return INSTANCE;
	}

	public WidgetInputClick(WidgetAnswer widgetAnswer, PacketProblemData problemData) {
		final HorizontalPanel panel = new HorizontalPanel();
		panel.setVerticalAlignment(ALIGN_MIDDLE);

		image = new Image(problemData.choice[0]);
		image.setPixelSize(Constant.CLICK_IMAGE_WIDTH, Constant.CLICK_IMAGE_HEIGHT);
		image.addMouseDownHandler(this);
		panel.add(image);

		buttonOk.setStyleName("gwt-Button-clickControl");
		panel.add(buttonOk);

		panel.add(canvas);

		add(panel);
	}

	public void enable(boolean b) {
		throughClick = !b;
		buttonOk.setEnabled(b);
	}

	public MarkedCanvas getMarkedCanvas() {
		return canvas;
	}

	public int[] getImageOffset() {
		final int x = image.getAbsoluteLeft();
		final int y = image.getAbsoluteTop();
		return new int[] { x, y };
	}

	protected void onLoad() {
		super.onLoad();

		final int imageAbsoluteLeft = image.getAbsoluteLeft();
		final int canvasAbsoluteLeft = canvas.getAbsoluteLeft();
		final int offsetX = imageAbsoluteLeft - canvasAbsoluteLeft;
		final int imageAbsoluteTop = image.getAbsoluteTop();
		final int canvasAbsoluteTop = canvas.getAbsoluteTop();
		final int offsetY = imageAbsoluteTop - canvasAbsoluteTop;
		canvas.setOffset(offsetX, offsetY);

		INSTANCE = this;

		timerUpdate.scheduleRepeating(UPDATE_DURATION);
	}

	protected void onUnload() {
		timerUpdate.cancel();
		INSTANCE = null;
		super.onUnload();
	}

	@Override
	public void onMouseDown(MouseDownEvent event) {
		if (throughClick) {
			return;
		}

		final int x = event.getX();
		final int y = event.getY();
		mouseX = x;
		mouseY = y;
		circleMarkId = canvas.addPointerMark(x, y, circleMarkId);
		canvas.update();
	}

	@Override
	public void onClick(ClickEvent event) {
		final Object sender = event.getSource();
		if (sender == buttonOk) {
			if (mouseX != Point.INVALID || mouseY != Point.INVALID) {
				sendAnswer(mouseX + " " + mouseY);
			} else {
				sendAnswer("");
			}
		}
	}
}
